package com.linewell.system.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.linewell.commom.dao.impl.BaseDaoImpl;
import com.linewell.system.dao.ISysRolesDao;
import com.linewell.system.model.SysRoles;
@Repository
public class SysRolesDaoImpl extends BaseDaoImpl<SysRoles> implements ISysRolesDao{

	@Override
	public boolean deleteAll(String ids) {
		String hql = "delete SysRoles s where s.id in ("+ids+") ";
		Query query = super.sessionFactory.getCurrentSession().createQuery(hql);
		//query.setString(0, ids);
		return (query.executeUpdate() > 0);
	}

	@Override
	public boolean removeAll(String ids) {
		// TODO Auto-generated method stub
		String hql = "update SysRoles s set s.status = 0 where s.id in ("+ids+") ";
		Query query = super.sessionFactory.getCurrentSession().createQuery(hql);
		return (query.executeUpdate() > 0);
	}

	@Override
	public List<SysRoles> getDep() {
		// TODO Auto-generated method stub
		String hql = " from SysRoles ";
		Query query = super.sessionFactory.getCurrentSession().createQuery(hql);
		return  query.list();
	}

	@Override
	public boolean updateAll(SysRoles c) {
		// TODO Auto-generated method stub
		
		StringBuffer hql = new StringBuffer(
				"update SysRoles s set ");
		
		//拼接修改条件
		
		if(c.getCreateTime()!= null && !"".equals(c.getCreateTime())){
			hql.append("  s.createTime = '"+c.getCreateTime()+"' ");
		}
		
		if(c.getSysDepartment() == null ){
			hql.append(" , s.sysDepartment.depId = null  ");
		}else{
			hql.append(" , s.sysDepartment.depId = "+c.getSysDepartment().getDepId()+" ");
		}
		
		
		if(c.getCreateUserId() != null && !"".equals(c.getCreateUserId())){
			hql.append(" , s.createUserId = '"+c.getCreateUserId()+"' ");
		}
//		if(c.getDepId() != null && !"".equals(c.getDepId() )){
//			hql.append(" , s.sysDepartment.depId = "+c.getSysDepartment().getDepId()+" ");
//		}
		if(c.getRemark() != null){
			hql.append(" , s.remark = '"+c.getRemark() +"' ");
		}
		if(c.getRoleName() != null && !"".equals(c.getRoleName())){
			hql.append(" , s.roleName = '"+c.getRoleName()+"' ");
		}
		if(c.getStatus() != null && !"".equals(c.getStatus())){
			hql.append(" ,s.status = "+c.getStatus()+" ");
		}
		if(c.getUpdateTime() != null && !"".equals(c.getUpdateTime())){
			hql.append(" , s.updateTime() = '"+c.getUpdateTime()+"' ");
		}
		if(c.getUpdateUserId() != null && !"".equals(c.getUpdateUserId())){
			hql.append(" , s.updateUserId = "+c.getUpdateUserId()+" ");
		}

		hql.append(" where s.id ="+c.getId()+" ");
		Query query = super.sessionFactory.getCurrentSession().createQuery(hql.toString());
		return (query.executeUpdate() > 0);
		
	}

	@Override
	public boolean saveAuthority(String mids,Integer gid) {
		String hql = "update SysRoles s set s.roleMenus = '"+mids+"' where s.id = ("+gid+") ";
		Query query = super.sessionFactory.getCurrentSession().createQuery(hql);
		return (query.executeUpdate() > 0);
	}

	@Override
	public List<SysRoles> getRoles(Integer depId) {
		// TODO Auto-generated method tub
		String hql;
		if(depId==null){
			
			hql="From SysRoles s";
			
		}else{
			hql="From SysRoles s where s.sysDepartment.depId="+depId;
			
		}
	
		List<SysRoles> sysRoles = super.sessionFactory.getCurrentSession().createQuery(hql).list();
	
		return sysRoles;
	}

}
